import 'package:flui/flui.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:lepig/model/SpecialModel.dart';
import 'package:lepig/util/JumpUtil.dart';

/**
 * 专题位
 */
class Topic extends StatelessWidget {
  Topic(this.lists);
  BuildContext context;
  List<SpecialModel> lists = [];

  @override
  Widget build(BuildContext context) {
    this.context=context;
    List<Widget> rowLists = [];
    lists.forEach((element) {
      rowLists.add(_rowItem(element));
    });
    return Column(
      children: rowLists.length==0?[
        FLSkeleton(
          shape: BoxShape.rectangle,
          borderRadius: BorderRadius.circular(2),
          height: 120,
        ),
        FLSkeleton(
          shape: BoxShape.rectangle,
          borderRadius: BorderRadius.circular(2),
          height: 120,
        ),
        FLSkeleton(
          shape: BoxShape.rectangle,
          borderRadius: BorderRadius.circular(2),
          height: 120,
        ),
      ]:rowLists.toList(),
    );
  }

  Widget _rowItem(SpecialModel specialModel) {
    List<Widget> columnLists = [];

    int i =0;
    specialModel.childs.forEach((element) {
      columnLists.add(_columItem(element, i, specialModel.childs.length));
      i++;
    });
    return Flex(
      direction: Axis.horizontal,
      children:columnLists.toList(),
    );
  }

  Widget _columItem(SpecialChilds specialChilds, int index, count) {
      if(count==2){
        return _columnTow(specialChilds)[index];
      }else if(count==3){
        return _columnThree(specialChilds,  index, count);
      }
  }

  List<Widget> _columnTow(SpecialChilds specialChilds){
     return [
       new Expanded(
         child:new InkWell(
           child:  new Container(
             color: Colors.white,
             margin: EdgeInsets.only(right: 10,bottom: 10),
             height: 120,
             child:  new Image.network(specialChilds.imgUrl,fit: BoxFit.fill),
           ),
           onTap: (){
             JumpUtil(this.context,jumpPositon: "",jumpName: specialChilds.title,jumpKey: specialChilds.jumpKey,jumpUrl:specialChilds.jumpUrl,isLocal: specialChilds.isLocal.toString()).go();
           },
         ),
       ),
       new InkWell(
         onTap: (){
           JumpUtil(this.context,jumpPositon: "",jumpName: specialChilds.title,jumpKey: specialChilds.jumpKey,jumpUrl:specialChilds.jumpUrl,isLocal: specialChilds.isLocal.toString()).go();
         },
         child: new Container(
           margin: EdgeInsets.only(bottom: 10),
           width: 120,
           height: 120,
           child: new Image.network(specialChilds.imgUrl,fit: BoxFit.fill),
         ),
       ),
     ];
  }
  Widget _columnThree(SpecialChilds specialChilds, int index, int count){
   return new Expanded(child: new Container(
      height: 120,
     margin: EdgeInsets.only(right: (index+1==count)?0:10),
      child: new InkWell(
        child: new Container(
          child: new Image.network(specialChilds.imgUrl,fit: BoxFit.fill,),
        ),
        onTap: (){
          JumpUtil(this.context,jumpPositon: "",jumpName: specialChilds.title,jumpKey: specialChilds.jumpKey,jumpUrl:specialChilds.jumpUrl,isLocal: specialChilds.isLocal.toString()).go();
        },
      ),
    ));
  }
}
